<?php 
class Cart extends Module
{
	static $cart_item_ids = '',$cart = array(), $cart_items = array(), $items = array();
	
	function Cart($row)
	{
		Module::Module($row);
		
		if(Url::get('cmd') == 'success')
		{
			require_once 'forms/CartSuccess.php';				
			$this->add_form(new CartSuccessForm());
			return ;
		}	
		
		Cart::$cart_item_ids = isset($_COOKIE['cart_items']) ? $_COOKIE['cart_items'] : '';
		
		if(CGlobal::$cart_sess_id =='')
		{	
			CGlobal::$cart_sess_id = session_id();
			AZLib::my_setcookie('cart_sess_id',session_id(),time()+3600*24*365);
		}
		
		$cart_items = '';
		
		Cart::$cart	= DB::select("cart","session='".CGlobal::$cart_sess_id."' AND status=1 ORDER BY id DESC");
		
		$cart_price 	= 0;
		$cart_quantity 	= 0;
			
			
		if(Cart::$cart)
		{
			$items = array();
			
			if(Cart::$cart_item_ids != '')
			{
				$re = DB::query("SELECT * FROM item WHERE id IN(".Cart::$cart_item_ids.") AND status=1 AND price>0 ORDER BY find_in_set(id,'".Cart::$cart_item_ids."')");
			
				if($re)
				{
					while($item = mysql_fetch_assoc($re))
					{
						$item['price'] = AZLib::convertCurrency($item['price'],$item['currency_id']);
						
						Cart::$items[$item['id']]	= $item;		
					}
				}
				
				$items = Cart::$items;
				
				$re = DB::query("SELECT * FROM cart_item WHERE cart_id=".Cart::$cart['id']." ORDER BY find_in_set(item_id,'".Cart::$cart_item_ids."')");
			}
			else
			{
				$re = DB::query("SELECT * FROM cart_item WHERE cart_id=".Cart::$cart['id']." ORDER BY id DESC");
			}
			
			$cart_item_del = '';
			
			if($re)
			{
				while($cart_item = mysql_fetch_assoc($re))
				{
					if(isset(Cart::$items[$cart_item['item_id']]))
					{
						if($cart_item['price'] != Cart::$items[$cart_item['item_id']]['price'] || $cart_item['sku'] != Cart::$items[$cart_item['item_id']]['sku'])
						{
							$cart_item['price'] = Cart::$items[$cart_item['item_id']]['price'];
							$cart_item['sku'] 	= Cart::$items[$cart_item['item_id']]['sku'];
							
							DB::update('cart_item',array('price'=>$cart_item['price'],'sku'=>$cart_item['sku']),"id={$cart_item['id']}");
						}
						
						if($cart_item['checked'])
						{
							$cart_price 	+= $cart_item['price'] * $cart_item['quantity'];
							$cart_quantity 	+= $cart_item['quantity'];
						}
						
						unset($items[$cart_item['item_id']]);
						
						$cart_items .= ($cart_items != '' ? ',' : '') . $cart_item['item_id'];
						
						Cart::$cart_items[$cart_item['id']]	= $cart_item;
					}
					else
					{
						$cart_item_del .= ($cart_item_del != '' ? ',' : '') . $cart_item['id'];
					}
				}
			}
			
			if($items)//Các item chưa thấy trong giỏ hàng
			{
				foreach($items as $item)
				{
					$cart_items .= ($cart_items != '' ? ',' : '') . $item['id'];
					
					$cart_item = array(
										'cart_id'	 	=>Cart::$cart['id'],
										'item_id' 		=>$item['id'],
										'sku' 			=>$item['sku'],
										'price'		 	=>$item['price'],
										'quantity'	 	=>1,
										'user_id'		=>(int)User::id(),
										'user_name'	 	=>User::user_name(),
										'time_add'	 	=>TIME_NOW,
										'time_change'	=>TIME_NOW,
										'status'		=>1,//1:Chưa TT; 2: Đã gửi đơn hàng ; 3: Đã TT; 4: Hủy đơn hàng; 5: Đã duyệt
										'checked'		=>1,
										);
										
					$cart_item['id'] = DB::insert("cart_item",$cart_item);					
					
					$cart_price 	+= $cart_item['price'];
					$cart_quantity 	+= 1;
					
					Cart::$cart_items[$cart_item['id']]	= $cart_item;		
					
					Cart::$items[$item['id']]	= $item;	
				}
			}
			
			if($cart_item_del != '')
			{
				DB::delete('cart_item',"id IN($cart_item_del)");
			}
		}
		elseif(Cart::$cart_item_ids !='')
		{
			Cart::$cart = 	array(
									'session'			=>CGlobal::$cart_sess_id,
									'ip'				=>$_SERVER['REMOTE_ADDR'],
									'time_create'		=>TIME_NOW,
									'time_change'		=>TIME_NOW,
									'time_finish'		=>0,
									'time_finish'		=>0,
									'user_id'			=>(int)User::id(),
									'user_name'			=>User::user_name(),
									'customer_name'		=>'',
									'customer_email'	=>'',
									'customer_phone'	=>'',
									'customer_address'	=>'',
									'customer_note'		=>'',
									'note'				=>'',
									'price'				=>0,
									'quantity'			=>0,
									'mm_user_id'		=>0,
									'mm_time_modify'	=>'',
									'mm_user_name'		=>'',
									'status'			=>1,//1:Chưa cập nhật;2:KH mới gửi; 3:Giỏ đã TT, đã TT; 4: Hủy đơn hàng; 5: Đã duỵêt đơn hàng
									);
								
			Cart::$cart['id'] = DB::insert("cart",Cart::$cart);
			
			$re 		= DB::query("SELECT * FROM item WHERE id IN(".Cart::$cart_item_ids.") AND status=1 AND price>0 ORDER BY find_in_set(id,'".Cart::$cart_item_ids."')");
		
			if($re)
			{
				while($item = mysql_fetch_assoc($re))
				{
					$item['price'] = AZLib::convertCurrency($item['price'],$item['currency_id']);
					
					$cart_items .= ($cart_items != '' ? ',' : '') . $item['id'];
					
					$cart_item = array(
										'cart_id'	 	=>Cart::$cart['id'],
										'item_id' 		=>$item['id'],
										'sku' 			=>$item['sku'],
										'price'		 	=>$item['price'],
										'quantity'	 	=>1,
										'user_id'		=>(int)User::id(),
										'user_name'	 	=>User::user_name(),
										'time_add'	 	=>TIME_NOW,
										'time_change'	=>TIME_NOW,
										'status'		=>1,//1:Chưa TT; 2: Đã gửi đơn hàng ; 3: Đã TT; 4: Hủy đơn hàng; 5: Đã duyệt
										'checked'		=>1,
										);
					$cart_price	 	+=	$item['price'];
					$cart_quantity 	+=	1;
									
					$cart_item['id'] = DB::insert("cart_item",$cart_item);					
					
					Cart::$cart_items[$cart_item['id']]	= $cart_item;		
					
					Cart::$items[$item['id']]	= $item;		
				}
			}
		}

		if(Cart::$cart['price'] != $cart_price || Cart::$cart['quantity'] != $cart_quantity)
		{
			Cart::$cart['price'] 	= $cart_price;
			Cart::$cart['quantity'] = $cart_quantity;
			
			DB::update("cart",array('price'=>$cart_price,'quantity'=>$cart_quantity),'id='.Cart::$cart['id']);
		}
		
		if($cart_items != Cart::$cart_item_ids)//Set lại cookies
		{
			Cart::$cart_item_ids = $cart_items;
			
			AZLib::my_setcookie('cart_items',Cart::$cart_item_ids,time()+3600*24*365);
		}
		
		$cmd	=	Url::get('cmd');
		
		switch($cmd)
		{
			case 'delete':			
						$id	=	Url::get('id',0);
		
						if($id && Cart::$cart && isset(Cart::$cart_items[$id]))
						{
							//echo Cart::$cart_item_ids,"<br /><br />";
							//echo "$id";
							
							System::debug(Cart::$cart_items);
							
							DB::delete('cart_item',"id=$id AND cart_id=".Cart::$cart['id']);
							
							DB::update("cart",array("time_change"=>TIME_NOW),"id=".Cart::$cart['id']);
							
							unset(Cart::$cart_items[$id]);
							
							Cart::$cart_item_ids = '';
							
							if(Cart::$cart_items)
							{
								//Cart::$cart_item_ids = implode(',',array_keys(Cart::$cart_items));
								
								foreach (Cart::$cart_items as $cart_item)
								{
									Cart::$cart_item_ids .= (Cart::$cart_item_ids != '' ? ',' : '') . $cart_item['item_id'];
								}
							}
				
							//echo Cart::$cart_item_ids,"<br /><br />";
							AZLib::my_setcookie('cart_items',Cart::$cart_item_ids,time()+3600*24*365);
						}
			
						$cart_price 	= DB::fetch("SELECT SUM(price * quantity) AS price_total FROM cart_item WHERE cart_id=".Cart::$cart['price']." AND checked=1 AND price>0 ANd quantity>0",'price_total',0);
						$cart_quantity 	= DB::fetch("SELECT SUM(quantity) AS price_quantity FROM cart_item WHERE cart_id=".Cart::$cart['price']." AND checked=1 AND price>0 ANd quantity>0",'price_quantity',0);
						
						if(Cart::$cart['price'] != $cart_price || Cart::$cart['quantity'] != $cart_quantity)
						{
							Cart::$cart['price'] 	= $cart_price;
							Cart::$cart['quantity'] = $cart_quantity;
							
							DB::update("cart",array('price'=>$cart_price,'quantity'=>$cart_quantity),'id='.Cart::$cart['id']);
						}
						
						Url::redirect_current();
						break;
			case 'finish':		
						require_once 'forms/CartFinish.php';				
						$this->add_form(new CartFinishForm());
						break;
				
			case '':
			default:	
						require_once 'forms/Cart.php';			
						$this->add_form(new CartForm());
						break;
		}
	}
}
?>